$(function () {
    $("#jqGrid").Grid({
        url: '../sys/unit/list',
        colModel: [
            {label: '单位ID', name: 'deptId', index: "dept_id", key: true, hidden: true},
            {label: '单位类型', name: 'rootType',index: 'root_type', width: 60, align: 'center', formatter: function (value) {
                    switch(value){
                        case 0:
                            return '旗舰店';
                            break;
                        case 1:
                            return '批发店';
                            break;
                        case 2:
                            return '私密型零售店';
                            break;
                        case 3:
                            return '公开型零售店';
                            break;
                    }
                }
            },
            {label: '单位名称', name: 'name', width: 75},
            {label: '手机', name: 'mobile', width: 75},
            {label: '电话', name: 'telephone', width: 90},
            {label: '微信', name: 'wechat', width: 100},
            {
                label: '背景图', name: 'appBgImageUrl', index: 'app_bg_image_Url', width: 100, align: 'center', formatter: function (value) {
                    return transBgImg(value);
                }
            },
            {label: '背景色', name: 'appBgColor', width: 100},
            {label: '字体色', name: 'appFontColor', width: 100}
        ]
    });
});

function transBgImg(url) {
    if (url) {
        return '<img width="178px" height="50px" src="' + url + '">';
    } else {
        return '-';
    }
};

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        dept: {
            parentName: null,
            parentId: 0,
            orderNum: 0
        },
        ruleValidate: {
            name: [
                {required: true, message: '单位名称不能为空', trigger: 'blur'}
            ]
        }
    },
    methods: {
        add: function () {
            vm.showList = false;
            vm.title = "新增";
        },
        update: function () {
            var unitId = getSelectedRow("#jqGrid");
            if (unitId == null) {
                return;
            }
            Ajax.request({
                url: "../sys/unit/info/" + unitId,
                async: true,
                successCallback: function (r) {
                    vm.showList = false;
                    vm.title = "修改";
                    vm.dept = r.dept;
                }
            });
        },
        del: function () {
            var unitId = getSelectedRow("#jqGrid");
            if (!unitId) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    type: "POST",
                    url: "../sys/unit/delete",
                    params: {"unitId": unitId},
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        saveOrUpdate: function (event) {
            var url = vm.dept.deptId == null ? "../sys/unit/save" : "../sys/unit/update";
            Ajax.request({
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.dept),
                type: 'POST',
                successCallback: function () {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleSuccess: function (res, file) {
            vm.dept.appBgImageUrl = file.response.url;
        },
        handleSuccessIcon: function (res, file) {
            vm.dept.icon = file.response.url;
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 1M。'
            });
        },
        eyeImage: function () {
            var url = vm.dept.appBgImageUrl;
            eyeImage(url);
        },
        eyeImageIcon: function () {
            var url = vm.dept.icon;
            eyeImage(url);
        }
    }
});
